﻿using System;
using GdNet.Integrations.NHibernate.Assistants;
using GdNet.Integrations.NHibernate.Mapping;
using GdNet.WebFramework.Domain.System;
using NHibernate.Mapping.ByCode;

namespace GdNet.WebFramework.Mapping.System
{
    public class ModuleMapping : AbstractJoinedSubclassMapping<Module, Guid>, IEntityMapping
    {
        public ModuleMapping()
            : base()
        {
            base.Property(e => e.Code);
            base.Property(e => e.Name);
            base.Property(e => e.Version);
            base.Property(e => e.AssemblyName);

            base.Bag(e => e.Actions, cm =>
            {
                cm.Access(Accessor.Field);
                cm.Cascade(Cascade.All | Cascade.DeleteOrphans);
                cm.Lazy(CollectionLazy.Lazy);
                cm.Key(km => km.Column(MappingAssistant.GetForeignKeyColumn<ModuleAction, Module>(ma => ma.Module)));
            }, m => m.OneToMany(mapping => mapping.NotFound(NotFoundMode.Exception)));
        }
    }
}
